﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 花店管理系统.geren
{
    public partial class Quanbupinglun : Form
    {

        private List<string> comments;
        private List<int> ratings;
        private BindingSource bindingSource;

        public Quanbupinglun()
        {
            InitializeComponent();
            bindingSource = new BindingSource();
            LoadCommentsFromDatabase();
        }

        private void LoadCommentsFromDatabase()
        {
            string query = @"SELECT O_pinglun AS Comment, O_dafen AS Rating FROM Torder";
            using (SqlConnection cn = new SqlConnection(DB.cn.ConnectionString)) // 确保使用正确的连接字符串
            {
                cn.Open();
                SqlCommand cmd = new SqlCommand(query, cn);
                SqlDataReader reader = cmd.ExecuteReader();
                comments = new List<string>();
                ratings = new List<int>();

                while (reader.Read())
                {
                    comments.Add(reader["Comment"].ToString());
                    if (!DBNull.Value.Equals(reader["Rating"]))
                    {
                        ratings.Add(Convert.ToInt32(reader["Rating"]));
                    }
                    else
                    {
                        ratings.Add(0);
                    }
                }
                reader.Close();

                bindingSource.DataSource = comments.Zip(ratings, (comment, rating) => new { Comment = comment, Rating = rating }).ToList();
            }
        }

        private void Quanbupinglun_Load(object sender, EventArgs e)
        {
            dataGridViewComments.DataSource = bindingSource;
            // 设置列标题和数据类型
            dataGridViewComments.Columns[0].HeaderText = "评论";
            dataGridViewComments.Columns[0].DataPropertyName = "Comment";
            dataGridViewComments.Columns[1].HeaderText = "评分";
            dataGridViewComments.Columns[1].DataPropertyName = "Rating";

            // 设置列宽度
            dataGridViewComments.Columns[0].Width = 400;
            dataGridViewComments.Columns[1].Width = 100;
        }

        private void btnCanle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
